#include<iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include "log.hpp"
#include <strings.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
Log log;
int main(int argc,char*argv[])
{
    if(argc!=3)
    {
        std::cout<<argv[0]<<" ip:192.168.130.1 port:8080"<<std::endl;
        exit(0);
    }
    //创建socket
    int socketfd=socket(AF_INET,SOCK_DGRAM,0);
    if(socketfd<0)
    {
        log.logmessage(Fatal,"socket error errno:%d errorstr:%S",errno,strerror(errno));
        exit(0);
    }
    log.logmessage(Info,"socket success socketfd:%d",socketfd);

    sockaddr_in server;
    socklen_t len=sizeof(server);
    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_addr.s_addr=inet_addr(argv[1]);
    server.sin_port=htons(std::stoi(argv[2]));
    char recive[1024];
    while(1)
    {
        std::string re;
        std::cout<<"Please Enter: ";
        std::cin>>re;
        int st= sendto(socketfd,re.c_str(),re.size(),0,(const sockaddr*)&server,len);
        if(st<1)
        {
            log.logmessage(Warning,"sendto error, errno:%d strerror:%s",errno,strerror(errno));
        }
        log.logmessage(Info,"sento success");

        int n=recvfrom(socketfd,recive,sizeof(recive)-1,0,(sockaddr*)&server,&len);
        if(n<0)
        {
            log.logmessage(Warning,"recfrom error errno:%d strerror:%s",errno,strerror(errno));
        }
        recive[n]=0;
        std::cout<<recive<<std::endl;
        
    }

    


    return 0;
}